package com.team.service.impl;

import com.team.dao.DeptMapper;
import com.team.dao.DutyMapper;
import com.team.dao.ReservationMapper;
import com.team.entity.Dept;
import com.team.entity.Duty;
import com.team.entity.Reservation;
import com.team.entity.User;
import com.team.service.AppointmentService;
import com.team.util.WebUtil;
import com.team.vo.AppointmentMessage;
import com.team.vo.PersonalInformation;
import com.team.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author keluoyisi
 * @program: seven
 * @description: 预约业务层实现
 * @date 2022-10-19 16:55:17
 */
@Service
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private DutyMapper dutyMapper;
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * @param :
     * @return Result
     * @description 显示所有部门以及对应的科室
     */
    @Override
    public Result selectDeptSection() {
        List<Dept> depts = deptMapper.queryAllDepts();
        return new Result(200,"查询成功",depts);
    }

    /**
     * @param id:
     * @return Result
     * @description 得到科室的id并且存在session中
     */
    @Override
    public Result addSectionId(int id) {
        WebUtil.getSession().setAttribute("appointmentSectionId",id);
        return new Result(200,"添加成功");
    }

    /**
     * @param appointmentMessage:
     * @return Result
     * @description 预约数量加1
     */
    @Override
    public Result addVisitCount(AppointmentMessage appointmentMessage) {
        int count = appointmentMessage.getCount();
        appointmentMessage.setCount(++count);
        WebUtil.getSession().setAttribute("appointmentMessage",appointmentMessage);
        dutyMapper.updateCount(appointmentMessage.getDid(),appointmentMessage.getCount());
        return new Result(200,"修改完成");
    }

    /**
     * @param :
     * @return Result
     * @description 预约数量减1
     */
    @Override
    public Result cancel() {
        AppointmentMessage appointmentMessage = (AppointmentMessage) WebUtil.getSession().getAttribute("appointmentMessage");
        int count=appointmentMessage.getCount()+1;
        dutyMapper.updateCount(appointmentMessage.getDid(),count);
        return new Result(200,"修改完成");
    }

    /**
     * @param personalInformation:
     * @return Result
     * @description 接收个人信息，创建预约订单
     */
    @Override
    public Result toRegistration(PersonalInformation personalInformation) {
        Object captcha = WebUtil.getSession().getAttribute("captcha");
        System.out.println("+++++++++++==++==++"+captcha);
        System.out.println(personalInformation.getCode());
        if (!captcha.equals(personalInformation.getCode())){
            return new Result(500,"验证码错误");
        }
        AppointmentMessage appointmentMessage = (AppointmentMessage) WebUtil.getSession().getAttribute("appointmentMessage");
        int id = (int) WebUtil.getSession().getAttribute("appointmentSectionId");
        User user = (User) WebUtil.getSession().getAttribute("user");

        Date date1 = new Date();
        String pnum =String.valueOf(date1.getYear())+date1.getMonth()+1+date1.getDate()+date1.getHours()+date1.getMinutes()+date1.getSeconds();
        Reservation reservation = new Reservation();
        reservation.setUserId(1);
        reservation.setPnum(pnum);
        reservation.setDutyId(appointmentMessage.getDid());
        reservation.setIdcard(personalInformation.getIdcard());
        reservation.setName(personalInformation.getName());
        reservation.setSex(personalInformation.getSex());
        reservation.setTel(personalInformation.getPhone());
        reservation.setSid(id);
        reservation.setVisitTime(appointmentMessage.getDate());
        reservation.setIspayment("0");
        reservation.setIsdelete("0");
        Date date = new Date(System.currentTimeMillis() );
        reservation.setCreateTime(date);
        WebUtil.getSession().setAttribute("reservation",reservation);
        reservationMapper.insertReservation(reservation);

        return new Result(200,"完成");
    }
}
